package com.newlink.building.userinfo.activity

import android.content.Context
import android.content.Intent
import android.os.Build
import android.text.TextUtils
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.webkit.JavascriptInterface
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.TextView
import com.google.gson.JsonSyntaxException
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityWebviewBinding

//import kotlinx.android.synthetic.main.activity_mBinding.webview.*

/**
 * @author wangyang
 * @date 2022/9/20
 * @description WebView加载H5页面
 * 1.帮助与反馈；
 * 2.条款与政策；
 */
class Module_UserInfo_WebViewActivityNL : NL_BaseActivity() {

    companion object {
        /**
         * 对外开放统一入口
         *
         * @param context
         * @param url     url
         * @param title   title
         */
        fun startSelf(context: Context?, url: String, title: String) {
            val intent = Intent(context, Module_UserInfo_WebViewActivityNL::class.java)
            intent.putExtra("link", url)
            intent.putExtra("title", title)
            context?.startActivity(intent)
        }
    }

//    override fun attachLayoutRes(): Int {
//        return R.layout.activity_webview
//    }

    private lateinit var mBinding: ActivityWebviewBinding

    override fun attachLayoutRes(): View {
        mBinding = ActivityWebviewBinding.inflate(layoutInflater)
        return layoutInflater.inflate(R.layout.activity_webview, null)
    }

    override fun initData() {

    }

    override fun initView() {
        initWebView(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        initWebView(intent)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mBinding.webview.canGoBack()) {
                mBinding.webview.goBack() //返回上个页面
                true
            } else {
                super.onKeyDown(keyCode, event)
            }
        } else super.onKeyDown(keyCode, event)
    }

    fun initWebView(intent: Intent?) {
        var webview_url = intent?.getStringExtra("link")
        var webview_title = intent?.getStringExtra("title")
        findViewById<TextView>(R.id.tv_title_bar_center).text = webview_title
        Log.e("webview_url", webview_url + "")

        if (!TextUtils.isEmpty(webview_url) && webview_url!!.contains("http")) {
            mBinding.webview.getSettings().setJavaScriptEnabled(true)
            mBinding.webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true)
            mBinding.webview.getSettings().setUseWideViewPort(true)
            mBinding.webview.getSettings().setLoadWithOverviewMode(true)
            mBinding.webview.getSettings().setAllowFileAccess(true)
            mBinding.webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE)
//            mBinding.webview.getSettings().setAppCacheEnabled(true)
//            mBinding.webview.getSettings().setAppCacheMaxSize((1024 * 1024 * 8).toLong())
//            val appCachePath = applicationContext.cacheDir.absolutePath
//            mBinding.webview.getSettings().setAppCachePath(appCachePath)
            mBinding.webview.getSettings().setDomStorageEnabled(true)
            mBinding.webview.getSettings().setDatabaseEnabled(true)
            mBinding.webview.getSettings().setSupportZoom(true)
            mBinding.webview.addJavascriptInterface(
                JsInterface(),
                "android"
            )
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mBinding.webview.getSettings()
                    .setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
            }
            mBinding.webview.loadUrl(webview_url)
            mBinding.webview.webChromeClient = MyWebChromeClient()
        }
    }

    inner class MyWebChromeClient : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            if (newProgress == 100) {
                //加载完网页进度条消失
                mBinding.pbLoad.visibility = View.GONE
            } else {
                //开始加载网页时显示进度条
                mBinding.pbLoad.visibility = View.VISIBLE;
                //设置进度值
                mBinding.pbLoad.progress = newProgress;
            }
        }
    }

    internal class JsInterface {
        /**
         * h5分享
         */
        @JavascriptInterface
        fun webZhumuShare(params: String?) {
//            H5ShareBean bean = null;
            try {
//                bean = new Gson().fromJson(params, H5ShareBean.class);
            } catch (e: JsonSyntaxException) {
                e.printStackTrace()
            }
            //
//            if (bean == null) {
//                return;
//            }
//
//            UmShare.getInstance().shareH5Info(H5Activity.this, bean);
        }
    }
}